#include <stdio.h>

extern int sgemm_ukernel_8_8_128b(int _m, int _n, int _k, float alpha, float* _a, float* _b, float* _c, int ldc, float beta, int ldb);

#define GEMM_P 128   //m
#define GEMM_Q 240   //k
//#define GEMM_R 12288 //n
#define GEMM_R 160
#define GEMM_UNROLL_M 8
#define GEMM_UNROLL_N 8

int sgemm_mNK(int m, int n, int k, float alpha, float *a, int lda, float *b, int ldb, float beta, float *c, int ldc){

    int min_i = 0, min_l = 0, min_j = 0;
    int js=0, ls=0, is=0;
    int jjs=0, min_jj = 0;
    int l1stride;
    float *a_temp = a;
    float *b_temp = b;
    float *c_temp = c;

    printf("起始地址: &a:%p  &b:%p  &c:%p\n", (void*)a, (void*)b, (void*)c);

    for(js = 0; js < n; js += GEMM_R){
        min_j = n - js;
        if (min_j > GEMM_R) min_j = GEMM_R;
        for(ls = 0; ls < k; ls += min_l){

            min_l = k - ls;

            if (min_l >= GEMM_Q * 2) {

                min_l  = GEMM_Q;
            } else {
            if (min_l > GEMM_Q) {
                min_l = ((min_l / 2 + GEMM_UNROLL_M - 1)/GEMM_UNROLL_M) * GEMM_UNROLL_M;
                                }
            }

            min_i = m;
            l1stride = 1;
            if (min_i >= GEMM_P * 2) {
                min_i = GEMM_P;
            } else {
            if (min_i > GEMM_P) {
                min_i = ((min_i / 2 + GEMM_UNROLL_M - 1)/GEMM_UNROLL_M) * GEMM_UNROLL_M;
            } else {
            l1stride = 0;
                    }
                    }
            
            for(jjs = js; jjs < js + min_j; jjs += min_jj){
    min_jj = min_j + js - jjs;
    if (min_jj >= 3*GEMM_UNROLL_N) min_jj = 3*GEMM_UNROLL_N; //循环展开大小
        else if (min_jj > GEMM_UNROLL_N) min_jj = GEMM_UNROLL_N;
                
    //printf("KERNEL_OPERATION 1 \n");
    //printf("min_i:%4d min_jj:%4d min_l:%4d ldc:%4d jjs:%4d  &a_temp:%p  &(b_temp+jjs):%p  &(c_temp+jjs):%p\n", min_i, min_jj, min_l, ldc, jjs, (void*)a_temp, (void*)(b_temp+jjs), (void*)(c_temp+jjs));
    //printf("#####################################\n");
    
                sgemm_ukernel_8_8_128b(min_i, min_jj, min_l, alpha, a_temp, (float *)(b_temp) + jjs, (float *)(c_temp) + jjs, ldc, beta, ldb);
            }                                               
                                            
  for(is =  min_i; is < m; is += min_i){
    min_i = m - is;

    if (min_i >= GEMM_P * 2) {
      min_i = GEMM_P;
    } else
      if (min_i > GEMM_P) {
        min_i = ((min_i / 2 + GEMM_UNROLL_M - 1)/GEMM_UNROLL_M) * GEMM_UNROLL_M;
      }

    //printf("KERNEL_OPERATION 2 \n");
    //printf("min_i:%4d min_j:%4d min_l:%4d ldc:%4d is:%4d js:%4d  &(a_temp+is*lda):%p  &(b_temp+js):%p  &(c_temp+is*ldc+js):%p\n", min_i, min_j, min_l, ldc, is, js, (void*)(a_temp+is*lda), (void*)(b_temp+js), (void*)(c_temp+is*ldc+js));
    //printf("#####################################\n");
      
    sgemm_ukernel_8_8_128b(min_i, min_j, min_l, alpha, (float *)(a_temp) + is * lda, (float *)(b_temp) + js, (float *)(c_temp) + is * ldc + js, ldc, beta, ldb);
}

    a_temp = a_temp + min_l;
    b_temp = b_temp + min_l * ldb;
    //printf("倒2循环  &a_temp:%p  &b_temp:%p\n", (void*)a_temp, (void*)b_temp);
  }

  a_temp = a;
  b_temp = b; 
  //printf("此时正在对 N 维度分块 重置a b地址\n");
}

a_temp = NULL;
b_temp = NULL;
c_temp = NULL;
return 0;
}